package com.taplinker.client.im.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.taplinker.core.Resource;
import com.taplinker.core.ResourceManager;
import com.taplinker.core.manager.InitializeContext;

public class SimpleDBManager implements InitializeContext,Resource{
	
		private static SimpleDBManager instance;
		private static DBManager dbManager;
		private static SQLiteDatabase db;
		
		public static final int VERSION_NO = 3;
		
		
		public static SimpleDBManager getInstance() {
			
			if (instance != null)
				return instance;
			synchronized (SimpleDBManager.class) {
				if (instance == null) {
					instance = new SimpleDBManager();
					ResourceManager.getInstance().addResource(instance);
				}
			}
			return instance;
		}

		public SQLiteDatabase getDataBase() throws SQLiteException
		{
			if(db == null)
			{
				throw new SQLiteException();
			}
			return db;
		}
		
		@Override
		public void releaseResource() {
			dbManager.close();
		}

		@Override
		public void initialize(Context context) {
			if(dbManager != null && db != null && db.isOpen())
				return;
			
			dbManager = new DBManager(context,"taplinker",null,1);
			instance = this;
			try
			{
				db =dbManager.getWritableDatabase();
			}
			catch(SQLiteException sqlException)
			{
				db = null;
			}
			catch(Exception e)
			{
				db = null;
			}
		}

		@Override
		public void clear() {
			// TODO Auto-generated method stub
			
		}
}

	class DBManager extends SQLiteOpenHelper{
		private final String conversationTable = "CREATE TABLE [conversation] ("+
				  "[conversationid] VARCHAR2(200), "+
				  "[visitorid] VARCHAR(20), "+
				  "[appname] VARCHAR(200), "+
				  "[updatetime] INT64, "+
				  "[baseurl] VARCHAR(200), "+
				  "[productid] VARCHAR(200), "+
				  "[productname] VARCHAR(200), "+
				  "[productpicture] VARCHAR(200), "+
				  "[description] TEXT, "+
				  "[username] VARCHAR(200), "+
				  "[userid] INT64, "+
				  "[unreadcount] INT DEFAULT 0, "+
				  "[userpicture] VARCHAR(200))";
		private final String messageTable = "CREATE TABLE [message] ("+
				  "[messageid] VARCHAR(200), "+
				  "[status] VARCHAR(10), "+
				  "[content] TEXT, "+
				  "[visitor] VARCHAR(200),"+ 
				  "[conversationid] VARCHAR(200),"+ 
				  "[messagetime] INT64,"+ 
				  "[userid] INT64,"+ 
				  "[username] VARCHAR(200), "+
				  "[read] BOOL, "+
				  "[messagetype] VARCHAR(10))";
		private final String chatUser = "CREATE TABLE [chatuser] ("+
					"id integer primary key autoincrement,"+
					  "[conversationid] VARCHAR(200), "+
					  "[username] VARCHAR(200), "+
					  "[userpicture] VARCHAR(200), "+
					  "[userid] INT64)";
		public DBManager(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
	       db.execSQL(conversationTable);
	       db.execSQL(messageTable);
	       db.execSQL(chatUser);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (newVersion!=oldVersion) {
	        db.execSQL("DROP TABLE IF EXISTS " + "chatuser");
	        db.execSQL("DROP TABLE IF EXISTS " + "message");
	        db.execSQL("DROP TABLE IF EXISTS " + "conversation");
	        onCreate(db);
		}
	}
}
